<?php

/*
*/

class Supplier_model extends CI_Model
{

	private $table = 'suppliers';

	public function __construct()
	{
		parent::__construct();
		//载入库文件
		$this->load->library('test_database');
		$this->test_database->test();
		//载入核心类
		$this->load->database($this->test_database->get_db());
		//载入库文件
		$this->load->library('form_validation');
		$this->load->library('session');
		//载入辅助类
		//$this->load->helper('cookie');
		//载入本地语言文件
		$this->lang->load('form_validation','chinese');
	}

	public function create()
	{
		//验证提交表单
		$this->form_validation->set_rules('supplier_name', '供应商', 'trim|required|min_length[3]|max_length[20]|unique[Supplier_model,supplier_name]|xss_clean');
		$this->form_validation->set_rules('supplier_priority', '优先级', 'trim|required|min_length[1]|max_length[1]|numeric|xss_clean');
		$this->form_validation->set_rules('supplier_contact', '联系方式', 'trim|required|min_length[8]|max_length[150]|is_contact');
		$this->form_validation->set_rules('supplier_location', '联系地址', 'trim|required|min_length[1]|max_length[150]|xss_clean');
		if ($this->form_validation->run())
		{
			$v['supplier_code'] = time();
			$v['supplier_name'] = $this->input->post('supplier_name');
			$v['supplier_priority'] = $this->input->post('supplier_priority');
			$v['supplier_contact'] = $this->encode_contact($this->input->post('supplier_contact'));
			$v['supplier_location'] = $this->input->post('supplier_location');
			$v['create_date'] = time();
			$v['update_date'] = $v['create_date'];
			//开始事务
			$this->db->trans_begin();
			$this->db->insert($this->table, $v);
			unset($v);
			$supplier_id = $this->db->insert_id();
			$this->db->where('supplier_id', $supplier_id);
			$v['supplier_code'] = time() . $supplier_id;
			$this->db->update($this->table, $v);
			if (1 == $this->db->affected_rows())
			{
				//提交SQL
				$this->db->trans_commit();
				return $supplier_id;
			}
			//回滚SQL
			$this->db->trans_rollback();
		}
		return false;
	}

	public function update()
	{
		//验证提交表单
		$this->form_validation->set_rules('supplier_name', '供应商', 'trim|required|min_length[3]|max_length[20]|unique[Supplier_model,supplier_name,self]|xss_clean');
		$this->form_validation->set_rules('supplier_priority', '优先级', 'trim|required|min_length[1]|max_length[1]|numeric|xss_clean');
		$this->form_validation->set_rules('supplier_contact', '联系方式', 'trim|required|min_length[8]|max_length[150]|is_contact');
		$this->form_validation->set_rules('supplier_location', '联系地址', 'trim|required|min_length[1]|max_length[150]|xss_clean');
		if ($this->form_validation->run())
		{
			$v['supplier_name'] = $this->input->post('supplier_name');
			$v['supplier_priority'] = $this->input->post('supplier_priority');
			$v['supplier_contact'] = $this->encode_contact($this->input->post('supplier_contact'));
			$v['supplier_location'] = $this->input->post('supplier_location');
			$v['update_date'] = time();
			$this->db->where('supplier_id', $this->input->post('supplier_id'));
			$this->db->update($this->table, $v);
			return true;
		}
		return false;
	}

	public function del($supplier_id)
	{
		$v['del_flg'] = 1;
		$this->db->where('supplier_id', $supplier_id);
		$this->db->update($this->table, $v);
		return true;
	}

	public function recover($supplier_id)
	{
		$v['del_flg'] = 0;
		$this->db->where('supplier_id', $supplier_id);
		$this->db->update($this->table, $v);
		return true;
	}

	//$del_flg[0,1,2]
	//$search[boolen]
	public function getAll($del_flg=2, $search=false)
	{
		if ($search)
		{
			return false;
		}
		if (0 == $del_flg || 1 == $del_flg) $this->db->where('del_flg', $del_flg);
		$this->db->order_by('del_flg', 'ASC');
		$this->db->order_by('supplier_priority', 'ASC');
		$query = $this->db->get($this->table);
		return $query->result();
	}

	public function getOne($id)
	{
		$query = $this->db->get_where($this->table, array('supplier_id'=>$id));
		if (1 == $query->num_rows())
		{
			foreach($query->result() as $v)
			{
				$v->supplier_contact = $this->decode_contact($v->supplier_contact);
				return get_object_vars($v);
			}
		}
		return false;
	}

	//验证唯一性拓展表单验证
	public function unique($value, $col, $self=false)
	{
		$this->db->where($col, $value);
		if ($self)
		{
			$this->db->where('supplier_id !=', $this->input->post('supplier_id'));
		}
		$query = $this->db->get($this->table, 1);
		if ($query->num_rows() == 0)
		{
			return true;
		}
		return false;
	}

	//Return Array, key value is the sub_goods_id
	public function get_some($supplier_ids)
	{
		$data = array();
		$this->db->where_in('supplier_ids', $supplier_ids);
		$query = $this->db->get($this->table);
		if ($query->num_rows() > 0)
		{
			foreach ($query->result() as $s)
			{
				$data[$s->supplier_id] = $s;
			}
			return $data;
		}
		else
		{
			$this->error = '存在无效的子货品';
			return false;
		}
	}

	//MY_Form_validation EX
	public function contact_validation($c, $convert=false)
	{
		//验证联系方式格式
		$t['contact_types'] = array('phone','qq','msn','mail');
		$t['contacts'] = explode(',', $c);
		//Here we go encode_json normal
		$c = '[';
		foreach ($t['contacts'] as $t['contact'])
		{
			$t['contact_info'] = explode('|', $t['contact']);
			if (!empty($t['contact_info'][0]) && !empty($t['contact_info'][1]))
			{
				$c = $c . '{"name":"' . $t['contact_info'][0] . '","contact":[';
				$t['contact_each'] = explode(':', $t['contact_info'][1]);
				foreach ($t['contact_each'] as $k=>$t['each'])
				{
					if ($k % 2 == 0)
					{
						if (in_array($t['each'], $t['contact_types']))
						{
							$c = $c . '{"type":"' . $t['contact_each'][$k] . '","content":"' . $t['contact_each'][$k+1] . '"},';
						}
						else
						{
							return false;
						}
					}
				}
				$c = substr($c, 0, -1) . "]},";
			}
			else
			{
				return false;
			}
		}
		if ($convert)
		{
			$c = substr($c, 0, -1) . ']';
			return $c;
		}
		else
		{
			return true;
		}
	}

	public function encode_contact($c)
	{
		return $this->contact_validation($c, true);
	}

	public function decode_contact($c)
	{
		if (empty($c)) return $c;
		$c = json_decode($c, true);
		//Here we go
		$sc = '';
		foreach ($c as $v)
		{
			$sc = $sc . $v['name'] . '|';
			foreach ($v['contact'] as $i)
			{
				$sc = $sc . $i['type'] . ':' . $i['content'] . ':';
			}
			$sc = substr($sc, 0, -1) . ',';
		}
		return substr($sc, 0, -1);
	}

}